package com.tchintech.pay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.model.entity.Card;
import com.tchintech.pay.utils.EditUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/9 0009.
 * 银行卡展示适配器
 */

public class CardListAdapter extends BaseAdapter {
    private Context context;
    private List<Card> list;

    public CardListAdapter(Context context,List<Card> list){
        this.context=context;
        this.list=list;
    }

    public void setList(List<Card> list){
        if(list==null){
            this.list=new ArrayList<Card>();
        }else{
            this.list=list;
        }
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Card getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViewHold vh = null;
        if (v == null) {
            vh = new ViewHold();
            v = LayoutInflater.from(context).inflate(R.layout.card_item, null);
            vh.bank_name= (TextView) v.findViewById(R.id.bank_name);
            vh.bank_num= (TextView) v.findViewById(R.id.bank_number);
            vh.bank_type= (TextView) v.findViewById(R.id.bank_type);
            v.setTag(vh);
        } else {
            vh = (ViewHold) v.getTag();
        }

        Card card=list.get(position);
        vh.bank_name.setText(card.getBank_name());
        vh.bank_type.setText(card.getCode_name());
        //设置银行卡输入监听
        EditUtil.initTextListener(vh.bank_num);
        //重新组装银行卡号
        String bank_num=card.getBank_number();
        String bank_head=bank_num.substring(0,4);
        String bank_last=bank_num.substring(bank_num.length()-3,bank_num.length());
        String bank_center=bank_num.substring(4,bank_num.length()-3);
        vh.bank_num.setText(bank_head+ChangeTx(bank_center)+bank_last);
        return v;
    }
    private   String ChangeTx(String str){
        String  newStr="";
        for(int i=0;i<str.length();i++){
            newStr=newStr+"*";
        }
        return newStr;
    }
    class ViewHold {
        TextView bank_name,bank_num,bank_type;


    }

}
